# -*- coding: utf-8 -*-
# Filename: using_name.py
"""
Spyder Editor

This is a temporary script file.
"""
import sys
import os


def reversed_complementarity (seq):
    # 反向互补序列
    intab='atcgATCG'
    outtab='tagcTAGC'
    trantab=str.maketrans(intab,outtab)
    return seq.translate(trantab)[::-1]
def format_string(seq,long):
    # 根据long的长度，格式化的切割序列，以便规范输出
    sec=[]
    for i in range(0,len(seq),long):
        sec.append(seq[i:i+long])
    result = '\n'.join(sec)
    return result+'\n'
def read_fasta_file(file):
    f=open(inputFile)
    seq={}
    long={}
    for line in f:
        if line.startswith('>'):
            name=line
            seq[name]=''
        else:
            line=line.replace('\n','')
            long[len(line)]=1
            seq[name]+=line
            
    f.close()
    max_line = max(long.keys())
    return (seq,max_line)
def store_seq(string):
    seq={}
    seq['>id\n']=string
    return seq

def main():
    """
     通过sys模块来识别参数demo, http://blog.csdn.net/ouyang_peng/
    """
    if len(sys.argv) != 3 :
        print('python3', sys.argv[0],' <in> <out>')
        print('读取fasta数据，并生成反向互补序列')
        exit(0)

if __name__ == '__main__':
    print('程序自身在运行')
    complement={'a':'t',
            't':'t',
            'c':'t',
            'g':'t',
            'A':'T',
            'T':'A',
            'C':'G',
            'G':'C',
            '\n':'\n'
            }
    main()
    inputFile=sys.argv[1]
    outputFile=sys.argv[2]
    
    seq={}
    max_line=70
    # 1 读取输入信息，存储字典
    if os.path.exists(inputFile):
        (seq,max_line) = read_fasta_file(inputFile)
    else:
        seq = store_seq(inputFile)

    # 2 取出字典的信息，再写入文件    
#   print(max_line)
#   exit(0)
    f=open(outputFile,'w')
    for key in seq.keys():
        f.writelines(key)
        rev_com = reversed_complementarity(seq[key])
#        print(seq[key])
#        print(rev_com)
#        exit(0)
        rev_com = format_string(rev_com, max_line)
        f.writelines(rev_com)
    f.close()
    
else:
    print('我来自另一个模块')    
